package com.fanrenqiji.modules.sys.dao;

import com.fanrenqiji.common.persistence.BaseDao;
import com.fanrenqiji.common.persistence.BaseDaoImpl;
import com.fanrenqiji.modules.sys.entity.Menu;
import com.fanrenqiji.modules.sys.entity.Role;
import com.fanrenqiji.modules.sys.entity.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年1月21日 下午5:14:18
 * @description
 * 菜单的数据处理程序
 */
public interface MenuDao extends MenuDaoCustom,CrudRepository<Menu, Long> {

	/**
	 * 查询所有的菜单节点
	 * @return
	 */
    @Query("from Menu where delFlag='" + Menu.DEL_FLAG_NORMAL + "' order by sort")
    public List<Menu> findAllList();
    /**
     * 删除当前菜单以及当前菜单的子菜单
     * @param id
     * @param likeParentIds
     * @return
     */
	@Modifying
	@Query("update Menu set delFlag='" + Menu.DEL_FLAG_DELETE + "' where id = ?1 or parentIds like ?2")
	public int deleteById(Long id, String likeParentIds);

	/**
	 * 通过当前登陆用户查询有效的菜单项目
	 * @param userId
	 * @return
	 */
    @Query("select distinct m from Menu m, Role r, User u where m in elements (r.menuList) and r in elements (u.roleList)" +
            " and m.delFlag='" + Menu.DEL_FLAG_NORMAL + "' and r.delFlag='" + Role.DEL_FLAG_NORMAL +
            "' and u.delFlag='" + User.DEL_FLAG_NORMAL + "' and u.id=?1" + // or (m.user.id=?1  and m.delFlag='" + Menu.DEL_FLAG_NORMAL + "')" +
            " order by m.sort")
    public List<Menu> findByUserId(Long userId);

	public List<Menu> findByParentIdsLike(String string);
	
	public List<Menu> findByParentIdsContaining(String string);
	

}

interface MenuDaoCustom extends BaseDao<Menu> {
}

@Repository
class MenuDaoImpl extends BaseDaoImpl<Menu> implements MenuDaoCustom{

}